package com.stox.ui.javafx.core.widget.wrapper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javafx.scene.input.KeyCombination;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.OrderComparator;
import org.springframework.core.Ordered;
import org.springframework.util.StringUtils;

import com.stox.util.ApplicationContextUtil;

public abstract class Menu extends javafx.scene.control.Menu implements InitializingBean, Ordered{

	public Menu(String name, String accelarator, String iconPath) {
		if(StringUtils.hasText(name)) 					super.setText(name);
		if(StringUtils.hasText(accelarator))			super.setAccelerator(KeyCombination.keyCombination(accelarator));
		if(StringUtils.hasText(iconPath)) 			super.setGraphic(new ImageView(iconPath));
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		Collection<MenuItem> menuItems = ApplicationContextUtil.getApplicationContext().getBeansOfType(MenuItem.class).values();
		List<MenuItem> children = new ArrayList<MenuItem>();
		for(MenuItem menuItem : menuItems){
			if(getClass().equals(menuItem.getParentClass())){
				children.add(menuItem);
			}
		}
		OrderComparator.sort(children);
		getItems().addAll(children);
	}
	
}
